package com.jessechanrui.template.req18.pdf.itext.pdf;

import bank.bod.util.FileUtil;
import cn.com.agree.afa.svc.javaengine.AppLogger;
import cn.com.agree.afa.svc.javaengine.context.JavaDict;
import com.alibaba.fastjson.JSONObject;
import ibs.server.jjp.scene.consts.JjpTradeConsts;
import lombok.Setter;
import platform.util.S_Date;

import java.util.ArrayList;

/**
 * @author lw
 *
 */
public class CommonJson extends PdfUtils{
	/** 第一页表格数据量*/
	private static final int ONENUM1 = 12;
	/**新页表格数据量*/
	private static final int OTHERNUM1 = 15;
	
	/** 第一页表格数据量*/
	private static final int ONENUM2 = 7;
	/**新页表格数据量*/
	private static final int OTHERNUM2 = 10;
	

	
	/**
	 * 生成Json文件-工人工资专项代发结果
	 * @param formList
	 * @param filePathName
	 */
	public static void packJsonFile1(JavaDict req, ArrayList<JavaDict> formList, String filePath, String fileName) {
		int size = formList.size();
		int rsNum = size-ONENUM1;
		int pageNum = size<=ONENUM1?1:((rsNum%OTHERNUM1==0?rsNum/OTHERNUM1:rsNum/OTHERNUM1+1)+1);
		StringBuilder content =new StringBuilder();
		content.append(STARTCHAR);
		
		packPage(content,20f,40f,20f,20f);
		
		packTitle(content,1,FONT1,23.0f,25.0f,20.0f,"工人工资专项代发结果");
		
		ArrayList<String> pdfHead = new ArrayList<String>();
		pdfHead.add("专户账号:");
		pdfHead.add(req.getStringItem("acctNo"));
		pdfHead.add("");
		pdfHead.add("专户名称:");
		pdfHead.add(req.getStringItem("acctName"));
		pdfHead.add("共 "+pageNum+" 页");
		pdfHead.add("");
		pdfHead.add("");
		pdfHead.add("交易日期:");
		pdfHead.add(S_Date.getSysDate());
		
		packHead(content,2,5,"1, 2, 2, 1, 2",pdfHead);
		//签章(调用无纸化盖章)
//		packSignature(content,"30, 30","450, 700");
		//数据表格
		for(int i = 0; i < pageNum; i++) {
			if(i!=0) {
				packPage(content,20f,40f,20f,20f);
			}
			String[] tableHead = new String[] {"渠道流水号","批次号","工资年月","交易日期","总笔数","总金额(元)","交易状态"};
			String[] keys = new String[] {"channelSerialNo","batchNo","salaryDate","platDate","totalNum","totalAmount","status"};
			int page = i+1;
			int count = 0;
			if(page != 1) {
				count = (page-2)*OTHERNUM1+ONENUM1;
			}
			AppLogger.info("第"+page+"页");
			AppLogger.info("从第"+count+"条开始");
			packTable(content,7,"1.3, 1, 1, 1, 0.8, 1.2, 1",page,count,formList,tableHead,keys,ONENUM1,OTHERNUM1);
			
			packPageNumber(content,page,"500, 30");
			
		}
		
		content.append(ENDCHAR);
		
		String contentStr = content.toString();
		FileUtil.writeContent(filePath, fileName, contentStr, false, "UTF-8");
	}
	
	/**
	 * 生成Json文件-工人工资专项代发结果详情
	 * @param formList
	 * @param filePathName
	 */
	public static void packJsonFile2(JavaDict req, ArrayList<JavaDict> formList,  String filePath, String fileName) {
		int size = formList.size();
		int rsNum = size-ONENUM2;
		int pageNum = size<=ONENUM2?1:((rsNum%OTHERNUM2==0?rsNum/OTHERNUM2:rsNum/OTHERNUM2+1)+1);
		StringBuilder content =new StringBuilder();
		content.append(STARTCHAR);
		//页
		packPage(content,20f,20f,20f,20f);
		//标题
		packTitle(content,1,FONT1,23.0f,25.0f,20.0f,"工人工资专项代发结果详情");
		//头部
		ArrayList<String> pdfHead = new ArrayList<String>();
		pdfHead.add("渠道流水号:");
		pdfHead.add(req.getStringItem("channelSerNo"));
		pdfHead.add("批次号:");
		pdfHead.add(req.getStringItem("batchNo"));
		pdfHead.add("专户账号:");
		pdfHead.add(req.getStringItem("acctNo"));
		pdfHead.add("专户名称:");
		pdfHead.add(req.getStringItem("acctName"));
		pdfHead.add("总笔数:");
		pdfHead.add(req.getStringItem("totalNum"));
		pdfHead.add("总金额(元):");
		pdfHead.add(req.getStringItem("totalAmount"));
		pdfHead.add("交易日期:");
		pdfHead.add(S_Date.getSysDate());
		pdfHead.add("");
		pdfHead.add("");
		pdfHead.add("共 "+req.getStringItem("pageNum")+" 页");
		
		packHead(content,5,4,"1, 2.5, 1, 2.5",pdfHead);
		
		packTitle(content,0,FONT1,12.0f,20.0f,10.0f,"如有跨行代发，请于提交五日后重新查询交易状态；如有跨行退汇，将会显示失败原因");
		//签章(改为调用无纸化盖章)
//		packSignature(content,"30, 30","450, 700");
		//数据表格
		for(int i = 0; i < pageNum; i++) {
			
			if(i!=0) {
				packPage(content,20f,20f,20f,20f);
			}
			String[] tableHead = new String[] {"序号","账户名","身份证号","开户行名称","账/卡号","发放金额(元)","发放年月","备注","交易状态","失败原因"};
			String[] keys = new String[] {"serNum","acctName","idNo","bankName","acctNo","amount","platDate","note1","hostStatus","errorMsg"};
			int page = i+1;
			int count = 0;
			if(page != 1) {
				count = (page-2)*OTHERNUM2+ONENUM2;
			}
			AppLogger.info("第"+page+"页");
			AppLogger.info("从第"+count+"条开始");
			packTable(content,10,"0.5, 1, 1, 1, 1, 1, 1, 1, 1, 1",page,count,formList,tableHead,keys,ONENUM2,OTHERNUM2);
			
			packPageNumber(content,page,"500, 30");
			
		}
		content.append(ENDCHAR);
		
		String contentStr = content.toString();
		FileUtil.writeContent(filePath, fileName, contentStr, false, "UTF-8");
		
	}

	/**
	 * 拼包页
	 */
	private static void packPage(StringBuilder content, float left, float right, float up, float down) {
		//页
		PdfReceiptVO vo = new PdfReceiptVO() {
			@Setter
			public String type;
			@Setter
			public float left;
			@Setter
			public float right;
			@Setter
			public float up;
			@Setter
			public float down;
		};
		vo.setType(PAGE);
		vo.setLeft(left);
		vo.setRight(right);
		vo.setUp(up);
		vo.setDown(down);
		String contentJson = JSONObject.toJSONString(vo);
		content.append(contentJson).append(COMMA);
	}
	
	/**
	 * 拼包标题
	 */
	private static void packTitle(StringBuilder content, int alignMent, String font, float fontSize, float spacingAfter, float spacingBefore, String value) {
		//标题
		PdfReceiptVO vo = new PdfReceiptVO() {
			@Setter
			public String type;
			@Setter
			public int alignMent;
			@Setter
			public String fontFamily;
			@Setter
			public float fontSize;
			@Setter
			public float spacingAfter;
			@Setter
			public float spacingBefore;
			@Setter
			public Object value;
		};
		vo.setType(PARAGRAPH);
		vo.setAlignMent(alignMent);
		vo.setFontFamily(font);
		vo.setFontSize(fontSize);
		vo.setSpacingAfter(spacingAfter);
		vo.setSpacingBefore(spacingBefore);
		vo.setValue(value);
		String contentJson = JSONObject.toJSONString(vo);
		content.append(contentJson).append(COMMA);
	}

	/**
	 * 拼包头部信息
	 */
	private static void packHead(StringBuilder content, int rowNum, int colNum, String widths,ArrayList<String> pdfHead) {
		//头部
		ArrayList<Object> tableList = new ArrayList<Object>();
		for (int i = 0; i < rowNum; i++) {
			ArrayList<PdfReceiptVO> rowList = new ArrayList<PdfReceiptVO>();
			for (int j = 0; j < colNum; j++) {
				int number = (i*colNum)+j;
				if(number<pdfHead.size()) {					
					rowList.add(creatTableCell(pdfHead.get(number),0,FONT1,10.0f));
				}
			}
			tableList.add(rowList);
		}
		PdfReceiptVO vo = new PdfReceiptVO() {
			@Setter
			public String type;
			@Setter
			public int columnNum;
			@Setter
			public String widths;
			@Setter
			public int border;
			@Setter
			public float spacingAfter;
			@Setter
			public float spacingBefore;
			@Setter
			public Object value;
		};
		vo.setType(TABLE);
		vo.setColumnNum(colNum);
		vo.setWidths(widths);
		vo.setBorder(0);
		vo.setSpacingAfter(10.0f);
		vo.setSpacingBefore(10.0f);
		vo.setValue(tableList);
		String contentJson = JSONObject.toJSONString(vo);
		content.append(contentJson).append(COMMA);
	}

	/**
	 * 拼包签章
	 */
	private static void packSignature(StringBuilder content, String scaleToFit, String absolutePosition) {
		//签章
		PdfReceiptVO vo = new PdfReceiptVO() {
			@Setter
			public String type;
			@Setter
			public String scaleToFit;
			@Setter
			public String absolutePosition;
		};
		vo.setType(SIGNATURE);
		vo.setScaleToFit(scaleToFit);
		vo.setAbsolutePosition(absolutePosition);
		String contentJson = JSONObject.toJSONString(vo);
		content.append(contentJson).append(COMMA);
	}
	
	/**
	 * 拼包表格
	 * @param formList 
	 */
	private static void packTable(StringBuilder content,int colNum,String widths,int page,int count,ArrayList<JavaDict> formList, String[] tableHead, String[] keys, int oneNum, int otherNum) {
		ArrayList<Object> dateList = new ArrayList<Object>();
		ArrayList<PdfReceiptVO> headList = new ArrayList<PdfReceiptVO>();
		for(String headName:tableHead) {	
			PdfReceiptVO cell = creatTableCell(headName,1,FONT2,10.0f);
			cell.setFontStyle(1);
			headList.add(cell);
		}
		dateList.add(headList);
		
		Boolean loop = true;
		
		while(loop) {
			JavaDict dict = formList.get(count);
			ArrayList<PdfReceiptVO> rowList = new ArrayList<PdfReceiptVO>();
			for(String key: keys) {	
				String value = dict.getStringItem(key);
				if(JjpTradeConsts.STATUS.equals(key) || JjpTradeConsts.HOSTSTATUS.equals(key) || JjpTradeConsts.ERRORMSG.equals(key)) {
					value = formatValue(key, value);
				}
				rowList.add(creatTableCell(value,1,FONT2,10.0f));
			}
			dateList.add(rowList);
			count++;
			if((page==1&&count==oneNum)||(count-oneNum)%otherNum==0||count>=formList.size()) {
				loop = false;
			}
		}
		PdfReceiptVO vo = new PdfReceiptVO() {
			@Setter
			public String type;
			@Setter
			public int columnNum;
			@Setter
			public String widths;
			@Setter
			public float padding;
			@Setter
			public Object value;
		};
		vo.setType(TABLE);
		vo.setColumnNum(colNum);
		vo.setWidths(widths);
		vo.setPadding(4.0f);
		vo.setValue(dateList);
		ArrayList<PdfReceiptVO> box1 = new ArrayList<PdfReceiptVO>();
		box1.add(vo);
		ArrayList<Object> box2 = new ArrayList<Object>();
		box2.add(box1);
		PdfReceiptVO vo1 = new PdfReceiptVO() {
			@Setter
			public String type;
			@Setter
			public float borderWidth;
			@Setter
			public float spacingAfter;
			@Setter
			public float spacingBefore;
			@Setter
			public Object value;
		};
		vo1.setType(TABLE);
		vo1.setBorderWidth(1.5f);
		vo1.setSpacingAfter(20.0f);
		vo1.setSpacingBefore(5.0f);
		vo1.setValue(box2);
		String contentJson = JSONObject.toJSONString(vo1);
		content.append(contentJson).append(COMMA);
		
	}


	/**
	 * 拼包页码
	 */
	private static void packPageNumber(StringBuilder content, int page, String absolutePosition) {
		//页码
		ArrayList<Object> pageList = new ArrayList<Object>();
		PdfReceiptVO vo = new PdfReceiptVO() {
			@Setter
			public Object value;
			@Setter
			public String fontFamily;
			@Setter
			public float fontSize;
		};
		vo.setValue("页码 "+page);
		vo.setFontFamily(FONT2);
		vo.setFontSize(9.0f);
		pageList.add(vo);
		vo = new PdfReceiptVO() {
			@Setter
			public String type;
			@Setter
			public String absolutePosition;
			@Setter
			public Object value;
		};
		vo.setType(PAGENUMBER);
		vo.setAbsolutePosition(absolutePosition);
		vo.setValue(pageList);
		String contentJson = JSONObject.toJSONString(vo);
		content.append(contentJson);
	}	
	
	/**
	 * 创建单元格
	 */
	public static PdfReceiptVO creatTableCell(String value,int alignMent,String fontFamily, float fontSize) {
		PdfReceiptVO vo = new PdfReceiptVO() {
			@Setter
			public String type;
			@Setter
			public int alignMent;
			@Setter
			public String fontFamily;
			@Setter
			public float fontSize;
			@Setter
			public Object value;
		};
		vo.setType(PARAGRAPH);
		vo.setAlignMent(alignMent);
		vo.setFontFamily(fontFamily);
		vo.setFontSize(fontSize);
		vo.setValue(value);
		return vo;
	}
	
	



}
